//
//  MYStatusCellTableViewCellFrame.m
//  奇闻
//
//  Created by 似水灵修 on 14-11-28.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYStatusCellTableViewCellFrame.h"
#import "MYStatus.h"
#import "MYUser.h"
@implementation MYStatusCellTableViewCellFrame
/**
 *  有新的奇闻数据就计算控件尺寸
 */
- (void)setStatus:(MYStatus *)status
{
    _status = status;
    //原创奇闻的frame
    [self setupOriginalFrame];
    CGFloat toolY = CGRectGetMaxY(_originalViewF);//工具条的y值
    if (status.retweeted_status) {// 判断有木有转发奇闻
        //转发奇闻的frame
        [self setupRetweetFrame];
        toolY = CGRectGetMaxY(_retweetViewF);
    }
    //工具条的frame
    _statusToolBarF = CGRectMake(0, toolY, MYScreenBounds.size.width, 35);
    //cell高度
    _cellH = CGRectGetMaxY(_statusToolBarF) + MYCellMargin;
}
//原创奇闻的frame
- (void)setupOriginalFrame
{
    //头像
    CGFloat iconXY = MYCellMargin;
    CGFloat iconWH = 35;
    _iconViewF = CGRectMake(iconXY, iconXY, iconWH, iconWH);
    //昵称
    CGFloat nameX = CGRectGetMaxX(_iconViewF) + MYCellMargin;
    CGFloat nameY = iconXY;
    CGSize nameSize = [_status.user.name sizeWithFont:MYNameFont];
    _nameViewF = (CGRect){{nameX, nameY}, nameSize};
    //会员图片
    if (_status.user.isVip) {
        CGFloat vipX = CGRectGetMaxX(_nameViewF) + MYCellMargin;
        CGFloat vipY = nameY;
        CGFloat vipWH = 14;
        _vipViewF = CGRectMake(vipX, vipY, vipWH, vipWH);
    }
    
    //内容
    CGFloat textX = MYCellMargin;
    CGFloat textY = CGRectGetMaxY(_iconViewF) + MYCellMargin;
    CGSize textSize = [_status.text sizeWithFont:MYTextFont maxSize:CGSizeMake(MYScreenBounds.size.width - 2 * MYCellMargin, MAXFLOAT)];
    _textViewF = (CGRect){{textX, textY}, textSize};
    
    // 配图相册
    CGFloat originH = CGRectGetMaxY(_textViewF) + MYCellMargin;
    if (_status.pic_urls.count) {
        CGFloat photosX = MYCellMargin;
        CGFloat photosY = originH;
        CGSize photosSize = [self photosSizeWithCount:_status.pic_urls.count];
        _photosViewF = (CGRect){{photosX, photosY}, photosSize};
        // 最大的配图加上一段间距
        originH = CGRectGetMaxY(_photosViewF) + MYCellMargin;
    }
    
    //整个原创奇闻的frame
    _originalViewF = CGRectMake(0, 0, MYScreenBounds.size.width, originH);
}
//计算配图相册的size
- (CGSize)photosSizeWithCount:(NSUInteger)count
{
    // 9 cols=3 rows = 3
    // 5 cols=3 rows =2
    // 4 cols=2 rows 2
    //获取总列数
    NSInteger cols = (count == 4) ? 2 : 3;
    //获取总行数
    NSInteger rows = (count - 1) / cols + 1;
    CGFloat photosW = cols * MYPhotoWH + (cols - 1) * MYPhotoMargin;
    CGFloat photosH = rows * MYPhotoWH + (rows - 1) * MYPhotoMargin;
    return CGSizeMake(photosW, photosH);
}
//转发奇闻的frame
- (void)setupRetweetFrame
{
    //昵称
    CGFloat nameX = MYCellMargin;
    CGFloat nameY = MYCellMargin;
    NSString *name =[NSString stringWithFormat:@"@%@",_status.retweeted_status.user.name];
    CGSize nameSize = [name sizeWithFont:MYNameFont];
    _retweekViewNameF = (CGRect){{nameX, nameY}, nameSize};
    //内容
    CGFloat textX = MYCellMargin;
    CGFloat textY = CGRectGetMaxY(_retweekViewNameF) + MYCellMargin;
    CGSize textSize = [_status.retweeted_status.text sizeWithFont:MYTextFont maxSize:CGSizeMake(MYScreenBounds.size.width - 2 * MYCellMargin, MAXFLOAT)];
    _retweekViewTextF = (CGRect){{textX, textY}, textSize};
    
    //转发奇闻
    CGFloat retweetY = CGRectGetMaxY(_originalViewF);
    CGFloat retweetH = CGRectGetMaxY(_retweekViewTextF) + MYCellMargin;
    _retweetViewF = CGRectMake(0, retweetY, MYScreenBounds.size.width, retweetH);
}
@end
